
/*
 * ************************************************************************
 * 本文件归属于西安协同时光软件有限公司，未经许可，不得擅自使用、拷贝和修改 项目组:金堆城钼业股份有限公司科研项目管理信息系统项目组 版 本: 1.0 日
 * 期：04/25/2010 作 者： Administrator
 * ************************************************************************
 */
package com.application.projectTeams.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.projectTeams.entity.Projectteams;
import com.application.projectTeams.struts.form.ProjectteamsForm;
import com.manager.BaseDao;
import com.manager.commons.page.PageBean;

/**
 * Copyrights 1997-2008 西安协同时光软件有限公司<br />
 * Description:ProjectteamsDAO<br />
 * Version:V0.1<br />
 * Author: Code Generator <br />
 * History: <br />
 * 04/25/2010 SyncBase Code Generator initial version.
 */
public class ProjectteamsDAO extends BaseDao {
	private static final Logger logger = Logger.getLogger(ProjectteamsDAO.class);

	private Projectteams projectteams;

	public Projectteams getProjectteams() {
		return this.projectteams;
	}

	public void setProjectteams(Projectteams projectteams) {
		this.projectteams = projectteams;
	}

	public Integer getAllCountProjectteams(ProjectteamsForm projectteamsForm) {
		// 储存查询条件的buffer
		StringBuffer buf = new StringBuffer();
		List args = new ArrayList();
		buf.append("select count(*) from Projectteams t,Projectworksheets p where t.worksheetsid=p.id ");
		// 查询字符串
		String strSql = getStringBuffer(projectteamsForm, buf,args).toString();

		return Integer.parseInt(this.getHibernateTemplate().find(strSql,args.toArray()).get(0).toString());
	}

	public void deleteProjectteamssByWorkSheetID(String workSheetsID) {
		String hql = " delete from Projectteams where worksheetsid = '" + workSheetsID + "'";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		}
		catch(RuntimeException re) {
			logger.error("deleteProjectteamssByWorkSheetID failed");
			throw re;
		}
		finally {
			if(session != null) {
				releaseSession(session);
			}
		}
	}
    
	public void deleteProjectteamss(ProjectteamsForm projectteamsForm) {
		String hql = " delete from Projectteams where id in  ( " + projectteamsForm.getIds() + ")";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		}
		catch(RuntimeException re) {
			logger.error("deleteProjectteamss failed");
			throw re;
		}
		finally {
			if(session != null) {
				releaseSession(session);
			}
		}
	}
	
	public List searchProjectteams(ProjectteamsForm projectteamsForm, final PageBean pageBean) {
		StringBuffer buf = new StringBuffer();

		// 返回list集合
		List list = null;
        final List args = new ArrayList();
		buf.append("select t,p from Projectteams t, Projectworksheets p where t.worksheetsid=p.id ");

		// 查询字符串条件
		buf = getStringBuffer(projectteamsForm, buf,args);

		// 传递查询条件buf值
		pageBean.setStringBuffer(buf);

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {

				Query query = session.createQuery(pageBean.getStringBuffer().toString() + " order by t.inputtime Desc ");

				// 设置开始页
				query.setFirstResult(pageBean.getPageSize() * (pageBean.getPageIndex() - 1));

				// 设置结束页
				query.setMaxResults(pageBean.getPageSize());
				setParameter(query, args);
				List resutlList = new ArrayList();
				List teamsList = query.list();
				if(teamsList != null && teamsList.size() > 0) {
					for(int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
						Projectteams teamsPojo = (Projectteams) objs[0];
//						Projectworksheets worksheets = (Projectworksheets) objs[1]; 
//						teamsPojo.setProjectname(worksheets.getProjectname());
//						teamsPojo.setProjectid(worksheets.getProjectid());
						resutlList.add(teamsPojo);
					}
				}
				return resutlList;
			}
		});
		return list;
	}
    
	public Projectteams getUpdateProjectteams(String id) {
		final StringBuffer buf = new StringBuffer();
		ProjectteamsForm projectteamsForm = new ProjectteamsForm();
		projectteamsForm.setId(id);
		// 返回list集合
		List list = null;

		buf.append("select t,p.projectname,p.projectid from Projectteams t, Projectworksheets p where t.worksheetsid=p.id and t.id = '" + id + "'");
		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {

				Query query = session.createQuery(buf.toString() + " order by t.inputtime Desc ");
				List resutlList = new ArrayList();
				List teamsList = query.list();
				if(teamsList != null && teamsList.size() > 0) {
					for(int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
						Projectteams teamsPojo = (Projectteams) objs[0];
						teamsPojo.setProjectname(objs[1].toString());
						teamsPojo.setProjectid(objs[2].toString());
						resutlList.add(teamsPojo);
					}
				}
				return resutlList;
			}
		});
		return list==null?null:(Projectteams) list.get(0);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(ProjectteamsForm projectteamsForm, StringBuffer buf,List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if(StringUtils.isNotBlank(projectteamsForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(projectteamsForm.getId());
		}
		if(StringUtils.isNotBlank(projectteamsForm.getProjectid())) {
			buf.append(" and p.projectid like ? ");
			args.add("%" + projectteamsForm.getProjectid() + "%");
		}
		if(StringUtils.isNotBlank(projectteamsForm.getDepartid())) {
			buf.append(" and t.departid like ? ");
			args.add("%" + projectteamsForm.getDepartid() + "%");
		}
		if(StringUtils.isNotBlank(projectteamsForm.getProjectname())) {
			buf.append(" and p.projectname like ? ");
			args.add("%" + projectteamsForm.getProjectname() + "%");
		}
		if(StringUtils.isNotBlank(projectteamsForm.getDepartname())) {
			buf.append(" and t.departname like ? ");
			args.add("%" + projectteamsForm.getDepartname()  + "%");
		}
		if(StringUtils.isNotBlank(projectteamsForm.getRole())) {
			buf.append(" and t.role like ? ");
			args.add("%" + projectteamsForm.getRole() + "%");
		}
//		buf.append(this.getSu().getCondition(8, projectteamsForm.getUser()));
		return buf;
	}
	
	
	public List getProjectTeamsByWorkSheetsID(Object workSheetID){
		StringBuffer sql = new StringBuffer();
		sql.append(" from Projectteams where worksheetsid = ?");
		return getHibernateTemplate().find(sql.toString(), workSheetID);
	}
	
	public List getProjectByResearcher(String id) {
		final StringBuffer buf = new StringBuffer();
		// 返回list集合
		List list = null; 
		buf.append(" select t,p from Projectworksheets t,Projectteams p where t.id=p.worksheetsid and p.employeeid='"+id+"' order by t.inputtime desc ");
		list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(buf.toString());
				List resutlList = new ArrayList();
				List teamsList = query.list();
				if(teamsList != null && teamsList.size() > 0) {
					for(int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
//						Projectworksheets teamsPojo = (Projectworksheets) objs[0];
//						Projectteams projectteams  = (Projectteams) objs[1];
//						teamsPojo.setBak4(projectteams.getRole());
//						resutlList.add(teamsPojo);
					}
				}
				return resutlList;
			}
			
		});
		return list;
	}
}
